//主管审核——商家信息审核
var pagesize = 10;
var pageindex = 1;
var sortfild = ""; //排序的关键字
var sortway = ""; //排序：升序、降序
var ttname = "商家信息审核";
$(function () {

    //字段过多的时候滑动
    $(".x-body").scroll(function (event) {
        console.info($(".my_customer").width()) + parseInt($(".x-body").scrollLeft())
        $(".crm_tab_wrap").width(parseInt($(".curse_tit").width()) + parseInt($(".x-body").scrollLeft()));
    });

    //下拉框
    $(document).on('click', ".my_select_ul li", function () {
        $(this).parent().siblings("div").find("span").html($(this).text());
        $(this).parent().siblings("div").find("span").attr("value", $(this).attr("value"));
        if ($(this).parent().siblings("div").find("span").hasClass("business_type_select")) {
            if ($.trim($(this).text()) == "全部") {
                $(".business_type_select").text("商户类型");
            }
        } else if ($(this).parent().siblings("div").find("span").hasClass("review_select")) {
            if ($.trim($(this).text()) == "全部") {
                $(".review_select").text("审核状态");
            }
        } else if ($(this).parent().siblings("div").find("span").hasClass("reviewer_select")) {
            if ($.trim($(this).text()) == "全部") {
                $(".reviewer_select").text("审核人");
            }
        }else if ($(this).parent().siblings("div").find("span").hasClass("appliacnt_select")) {
            if ($.trim($(this).text()) == "全部") {
                $(".appliacnt_select").text("申请人");
            }
        }
        //获取数据
        $("#first_button").click();
    });


    //排序
    $(".list_sort_tab").click(function () {
        sortfild = $(this).attr("value");
        if ($(this).find("i").hasClass("icon_downs")) {
            $(this).find("i").removeClass("icon_downs");
            $(this).find("i").addClass("icon_ups");
            sortway = "asc";
        } else {
            $(this).find("i").removeClass("icon_ups");
            $(this).find("i").addClass("icon_downs");
            sortway = "desc";
        }
        //必须要，否则排序则不能调用该方法  使用该句，当点击排序，就同时点击了 第一页的按钮，自动调用分页的效果，
        $("#first_button").click();
    });

    //悬浮显示 当宽度小于 文字宽度时，则鼠标移到该内容时，悬浮显示，将文字全部显示出来
    $(document).on("mouseover", ".show_tips", function () {
        if ($(this).width() > $(this).parent().width()) {
            layer.tips($(this).html(), $(this), {
                tips: [1, '#3595CC'],
                time: 0
            })
        }
    });
    $(document).on("mouseout", ".show_tips", function () {
        layer.closeAll('tips');
    });

    getData();
})


function getData() {
    getlist();
}

//获取列表
function getlist() {
    var pagesize = $("#selectPageSize1").text();
    var pageindex = $("#currentPage").text();
    var searchval = $("#conditional_searchinput").val().trim(); //搜索内容
    var field = $("#search_name").attr("value"); //搜索字段
    var reviewStatus = $("#check_select_name").attr("value");//审核状态下拉
    var business_type = $("#business_type_name").attr("value");//商户类型下拉
    var appliacnt = $("#appliacnt_select_name").attr("value");//申请人下拉
    var reviewer = $("#reviewer_select_name").attr("value");//审核人下拉

    var layLoad = layer.load(2);
    $("#biuuu_city_list").empty();
    $.post('/businesinfo/checkbusinessinfo',
        {
            "pagesize": pagesize,
            "pageindex": pageindex,
            "searchval": searchval,
            "searchkey": field,
            "sortway": sortway,
            "sortfild": sortfild,
            "appliacnt": appliacnt,
            "business_type": business_type,
            "reviewStatus": reviewStatus,
            "reviewer": reviewer
        }, function (value) {
            layer.close(layLoad);
            if (value.status == 0 && value.msg == "ok") {
                var list = value.list;
                var reviewlist = value.reviewlist;
                var applylist = value.applylist;

                var pagecount = value.pagecount;
                var recordcount = value.recordcount;
                $("#totalSize").html(recordcount);
                $("#totalPage").html(pagecount);

                if (applylist.length>0){
                    var str = "";
                    for (var i=0;i<applylist.length;i++){
                        str += "<li value='"+ applylist[i].applicant_encryptId +"'><a href='javascript:void(0)'><span class='show_tips'>"+ applylist[i].appliantName +"</span></a></li>";

                    }
                    if(str != ""){
                        var ul_str = "<li value=''><a href='javascript:void(0);'><span class='show_tips'>全部</span></a></li>"+str;
                    }
                    $("#appliacnt_select_ul").html(ul_str);
                }

                if (reviewlist.length>0){
                    var str = "";
                    for (var i=0;i<reviewlist.length;i++){
                        str += "<li value='" + reviewlist[i].reviewer_encryptId + "'><a href='javascript:void(0);'><span class='show_tips'>"+ reviewlist[i].reviewerName + "</span></a></li>";
                    }
                    if(str != ""){
                        var ul_str  = "<li value=''><a href='javascript:void(0);'><span class='show_tips'>全部</span></a></li>"+str;
                    }
                    $("#reviewer_select_ul").html(ul_str);
                }

                if (list.length == 0) {
                    $("#biuuu_city_list").prepend("<tr class=\"iframe_tr iframe_bg\"><td colspan=\"13\" style=\"width:1126px;text-align:center;\">暂无数据</td></tr>");
                } else {
                    var htm = "";
                    for (var i = 0; i < list.length; i++) {
                        htm += "<tr>";
                        htm += "<td><span class='show_tips'>" + (list[i].applicantName == null ? "" : list[i].applicantName) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_name == null ? "" : list[i].business_name) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].CompanyName == null ? "" : list[i].CompanyName) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_Legal == null ? "" : list[i].business_Legal) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_tel == null ? "" : list[i].business_tel) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_license == null ? "" : '<img src="' + list[i].business_license + '?x-oss-process=style/tuImgStyle" layer-src="' + list[i].business_license + '" >') + "</span ></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_Credit == null ? "" : list[i].business_Credit) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_type == null ? "" : getBusinessTypeName(list[i].business_type)) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_createtime == null ? "" : list[i].business_createtime) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].remarks == null ? "" : list[i].remarks) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].business_state == null ? "" : getstateName(list[i].business_state)) + "</span></td>";
                        htm += "<td><span class='show_tips'>" + (list[i].reviewerName == null ? "" : list[i].reviewerName) + "</span></td>";

                        htm += "<td class='td-manage'>";
                        if(list[i].business_state == "0"){
                            htm += "<a href=\"javascript:void(0)\" onclick=\"ReviewWin('" + list[i].business_encryptId + "')\">审核</a></div>";
                        }else {
                            htm += "<a href=\"javascript:void(0)\" onclick=\"withdraw('" + list[i].business_encryptId + "')\">撤回</a></div>";
                        }
                         htm += "</td>";

                        htm += "</tr>";
                    }
                    $("#biuuu_city_list").html(htm);

                }
            } else {
                if (value.msg == null || value.msg == "") {
                    layer.alert("接口出现错误！", {icon: 2})
                } else {
                    layer.alert(value.msg, {icon: 2})
                }
            }
        })
}

//根据商户类型id，返回商户类型名称
function getBusinessTypeName(business_type) {
    if (business_type != "" && business_type!=null){
        if(business_type == "1"){
            return "农家乐";
        }else if(business_type == "2"){
            return "旅游景区";
        }
    }else {
        return "";
    }
}

function getstateName(state) {
    if(state != "" && state!= null){
        if(state == 0){
            return "未审核";
        }else if(state == 1){
            return "已通过";
        }else if(state == 2){
            return "已拒绝";
        }
    }else {
        return "";
    }
}

//拒绝
function ReviewWin(business_encryptId) {
    $("#add_business_encryptId").attr("value",business_encryptId);
    ComOpenWin(1, "审核", "500px", 'auto', $("#refuseReason"), 1); //拒绝理由弹框
}

//审核-提交申请按钮
function question_reviewer(type) {
     var business_encryptId = $("#add_business_encryptId").attr("value");
     var remark = $("#remark").val(); // 备注信息
    if(type != ""){
        var ii = layer.load(2);
        $.post('/businesinfo/checkOperate', {"business_encryptId": business_encryptId,"type":type,"remark":remark}, function (data) {
            layer.close(ii);
            if (data.status == 0 && data.msg == 'ok') {
                layer.msg("操作成功！",{time:2000,icon:1});
                $("#first_button").click();
                mclose();
            } else {
                if(data.msg==null||data.msg==""){
                    layer.alert("接口出现错误！", {icon: 2})
                }else{
                    layer.alert(data.msg, {icon: 2})
                }
            }
        })
    }
}

//撤回操作
function withdraw(business_encryptId) {
    if(business_encryptId != ""){
        var ii = layer.load(2);
        $.post('/businesinfo/checkRevoke', {"business_encryptId": business_encryptId}, function (data) {
            layer.close(ii);
            if (data.status == 0 && data.msg == 'ok') {
                layer.msg("操作成功！",{time:2000,icon:1});
                $("#first_button").click();
                mclose();
            } else {
                if(data.msg==null||data.msg==""){
                    layer.alert("接口出现错误！", {icon: 2})
                }else{
                    layer.alert(data.msg, {icon: 2})
                }
            }
        })
    }
}


//点击图片，进行放大
$(document).on("click", "#biuuu_city_list tr td", function () {
    var url = $(this).find("img").attr("layer-src");
    if(url != "" && url != undefined && url != "undefined"){
        seePicture(url);
    }
})

//图片放大
function seePicture(url) {
    var url = url.split(",");
    var photos = new Array();
    for (var i = 0; i < url.length; i++) {
        photos.push({"alt": "图片" + (1 + i), "pid": "", "src": url[i], "thumb": ""});
    }
    var json = {"title": "素材", "id": "", "start": 0, "data": photos};
    layer.photos({
        photos: json,
        area: "[auto,auto]",
        skin: "layer_photos"
        , shift: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
}